package com.concerneyes.mine.adapter;

import java.util.ArrayList;
import java.util.List;


import android.content.Context;
import android.graphics.Paint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.concerneyes.CEApp;
import com.concerneyes.R;
import com.concerneyes.mine.model.OrderListModel;

import http.JsonMap;
import util.ImageUtil;

public class MyOrderViewAdapter extends BaseAdapter implements OnClickListener {
    private Callback mCallback;
    private Context mContext;
    private String mOrderStyle;
    private String preson;
    ViewHolder holder = null;
    private List<OrderListModel> datas;

    public interface Callback {
        public void click(View v, String text);
    }

    public MyOrderViewAdapter(Context context, Callback callback, String orderStyle, String preson) {
        datas = new ArrayList<OrderListModel>();
        this.mCallback = callback;
        this.mContext = context;
        this.mOrderStyle = orderStyle;
        this.preson = preson;
    }

    public void clear() {
        datas.clear();
    }

    public void addDatas(List<OrderListModel> datas) {
        if (datas != null) {
            this.datas.addAll(datas);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public OrderListModel getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_order_list, null);
            holder.orderbtnLeft = (TextView) convertView.findViewById(R.id.order_btn_left);
            holder.orderbtnRight = (TextView) convertView.findViewById(R.id.order_btn_right);
            holder.orderGoodsName = (TextView) convertView.findViewById(R.id.order_goods_name);
            holder.orderGoodsNum = (TextView) convertView.findViewById(R.id.order_goods_num);
            holder.orderGoodsSize = (TextView) convertView.findViewById(R.id.order_goods_size);
            holder.tv_remake = (TextView) convertView.findViewById(R.id.tv_remake);
            holder.orderImg = (ImageView) convertView.findViewById(R.id.order_img);
            holder.ll_btn_order = (LinearLayout) convertView.findViewById(R.id.ll_btn_order);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        String[] orderStyle = mContext.getResources().getStringArray(R.array.order_me_style);
        if (mOrderStyle.equals(orderStyle[0])) {
            holder.orderbtnLeft.setText(R.string.look_order);
            holder.orderbtnRight.setText(R.string.perfect);
            holder.tv_remake.setText(R.string.my_order_remake1);
            holder.tv_remake.setTextColor(mContext.getResources().getColor(R.color.app_red));
        } else if (mOrderStyle.equals(orderStyle[1])) {
            holder.orderbtnLeft.setText(R.string.look_order);
            holder.orderbtnRight.setVisibility(View.GONE);
            holder.tv_remake.setText(R.string.my_order_remake2);
        } else if (mOrderStyle.equals(orderStyle[2])) {
            holder.orderbtnLeft.setText(R.string.look_order);
            holder.orderbtnRight.setText(R.string.look_log);
            holder.tv_remake.setText(R.string.my_order_remake3);
        } else if (mOrderStyle.equals(orderStyle[3])) {
            holder.orderbtnLeft.setText(R.string.look_order);
            holder.orderbtnRight.setVisibility(View.GONE);
            holder.tv_remake.setText(R.string.my_order_remake4);
        }else  if (mOrderStyle.equals(orderStyle[4])) {
            holder.orderbtnLeft.setText(R.string.look_order);
            holder.orderbtnRight.setVisibility(View.GONE);
            holder.tv_remake.setText(R.string.my_order_remake5);
            holder.tv_remake.setTextColor(mContext.getResources().getColor(R.color.app_red));
        }else {
            holder.orderbtnLeft.setText(R.string.look_order);
            holder.orderbtnRight.setVisibility(View.GONE);
            holder.tv_remake.setText(R.string.my_order_remake6);
        }
        OrderListModel data = getItem(position);
        String cover = data.getProduct().getImgList().get(0).getPath();
        ImageUtil.glideloadImage(CEApp.getResLink(cover), holder.orderImg, R.drawable.ic_text);
        String title = data.getProduct().getTitle();
        holder.orderGoodsName.setText(title);
        String price = data.getPrice();
        holder.orderGoodsSize.setText("¥"+price);


        holder.orderbtnLeft.setOnClickListener(this);
        holder.orderbtnLeft.setTag(position);
        holder.orderbtnRight.setOnClickListener(this);
        holder.orderbtnRight.setTag(position);
        return convertView;
    }

    class ViewHolder {
        TextView orderGoodsName;
        TextView orderGoodsSize;
        TextView orderGoodsNum;
        TextView orderbtnLeft;
        TextView orderbtnRight;
        TextView tv_remake;
        ImageView orderImg;
        LinearLayout ll_btn_order;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.order_btn_left:
                mCallback.click(v, holder.orderbtnLeft.getText().toString());
                break;
            case R.id.order_btn_right:
                mCallback.click(v, holder.orderbtnRight.getText().toString());
                break;

            default:
                break;
        }

    }

}
